//
//  ViewController.m
//  SetTheOperator
//
//  Created by 赵忠鹏 on 2019/3/13.
//  Copyright © 2019 zhaozhongpeng. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@property (nonatomic,weak) NSArray *personArr;

@property (nonatomic,weak) Person *personOne;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    Person *personOne = [[Person alloc] initWithAge:1 andName:@"one"];
    self.personOne = personOne;
    
    Person *personTwo = [[Person alloc] initWithAge:2 andName:@"two"];
    
    Person *personThree = [[Person alloc] initWithAge:3 andName:@"two"];
    
    
    NSArray *personArr = @[personOne,personTwo,personThree];
    
    self.personArr = personArr;
    
    [self simple];
    
    [self objectZPPerson];
    
    
    
}


- (void)objectZPPerson{
    
    // 对象集合运算符：
    NSArray *unionArr = [self.personArr valueForKeyPath:@"@unionOfObjects.name"];
    // unionArr @[one,two,two]; 不去重
    
    [unionArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"unionArr - %@",obj);
    }];
    
    NSArray *distinctUnionArr = [self.personArr valueForKeyPath:@"@distinctUnionOfObjects.name"];
    // distinctUnionArr @[one,two];  去重
    
    [distinctUnionArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"distinctUnionArr - %@",obj);
    }];
}

- (void)simple{
    //简单集合对象
    NSInteger avg = [[self.personArr valueForKeyPath:@"@avg.age"] integerValue];
    NSInteger sum = [[self.personArr valueForKeyPath:@"@sum.age"] integerValue];
    NSInteger max = [[self.personArr valueForKeyPath:@"@max.age"] integerValue];
    NSInteger min = [[self.personArr valueForKeyPath:@"@min.age"] integerValue];
    NSInteger count = [[self.personArr valueForKeyPath:@"@count.self"] integerValue]; // .self 写不写都行
    
    // 集合属性
    NSInteger spcCount = [[self.personOne valueForKeyPath:@"arr.@sum.manAge"] integerValue];
    
    NSLog(@"\n集合属性\n%ld\n",(long)spcCount);
    
    NSLog(@"\n简单集合对象\n avg - %ld \n sum - %ld\n max - %ld \n min - %ld \n count - %ld\n",(long)avg,(long)sum,(long)max,(long)min,(long)count);
}

@end
